from django.views import View

from django.views import View

from User.models import Order, OrderGoods, OrderRent
from Venue.models import Goods, Rent
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class ShopCarBuyView(LoginRequiredUser, View):
    """ 购物车结算信息 """

    @Req.getJson
    def post(self, request):
        """ 添加或更改 """
        type = request.json_dict.get('type')
        list = request.json_dict.get('list')

        user = request.user.user

        if type not in Order.OrderType.list():
            return Res.Json(Res.ParamErr)
        if len(list) ==0:
            return Res.Json(Res.ParamErr)

        order = None
        if type == Order.OrderType.Goods:  # 商品
            for item in list:
                goodsId = item['id']
                number = item['number']
                goods = Goods.objects.filter(id=goodsId).first()
                order = OrderGoods.create(user,goods,number=number,message='', order=order)
                user.popShopCar(goodsId,type)
        elif type == Order.OrderType.Rent:  # 租赁
            for item in list:
                rentId = item['id']
                number = item['number']
                rent = Rent.objects.filter(id=rentId).first()
                order = OrderRent.create(user, rent, number=number, message='', order=order)
                user.popShopCar(rentId, type)

        return Res.Json(Res.OK,result=order.Info())
